package com.howells.time.module.first.view;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.WindowManager;

import com.howells.time.R;
import com.howells.time.base.activity.BaseActivity;
import com.howells.time.base.http.ServerAddress;
import com.howells.time.base.utils.DES;
import com.howells.time.base.utils.SharedPrefsUtil;
import com.howells.time.module.UIHelper;
import com.howells.time.module.first.presenter.FirstPresenter;
import com.howells.time.module.first.presenter.FirstPresenterImp;
import com.howells.time.module.main.model.User;


public class FirstActivity extends BaseActivity implements FirstView {

    private FirstPresenter firstPresenter;
    private boolean islogin;
    private User info;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        firstPresenter =new FirstPresenterImp(this);
        super.onCreate(savedInstanceState);
        //去掉Activity上面的状态栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_first);
        islogin=false;
        String user= SharedPrefsUtil.getValue(this, "UserInfo", "username", null);
        String psw=SharedPrefsUtil.getValue(this, "UserInfo", "password", null);
        if(user!=null&&psw!=null) {
            info=new User(user,new DES().decrypt(psw));
            islogin= true;
        }

        String server_ip=SharedPrefsUtil.getValue(getApplicationContext(),"server_config","server_ip",null);
        String server_port=SharedPrefsUtil.getValue(getApplicationContext(),"server_config","server_port",null);
        String server_name=SharedPrefsUtil.getValue(getApplicationContext(),"server_config","server_name",null);

        if(server_ip!=null&&server_port!=null){
            ServerAddress.ServerIp.setCode(server_ip);
            ServerAddress.ServerPort.setCode(server_port);
            ServerAddress.ServerName.setCode(server_name);
        }

        new Handler().postDelayed(new StartHandler(), 300);
    }

    @Override
    public void jumpSuccess() {
        UIHelper.jumpLogin(this);
    }

    @Override
    public void jumpMain() {
        UIHelper.jumpMain(this,new Intent());
    }


    class StartHandler implements Runnable{

        @Override
        public void run() {
            if(islogin){
                firstPresenter.jumpmain(info);
            }else {
                firstPresenter.jumplogin();
            }
        }
    }
}
